package com.lbd.finance.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.finance.bean.WebTradeDetail;
import com.lbd.finance.bean.vo.WebTradeDetailVo;
import com.lbd.finance.dao.WebTradeDetailRepository;
import com.lbd.finance.service.WebTradeDetailService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 平台交易记录
 */
@Service
@Transactional
public class WebTradeDetailServiceImpl extends BaseServiceImpl<WebTradeDetail, WebTradeDetailRepository> implements WebTradeDetailService {
    @Autowired
    WebTradeDetailRepository webtradedetailRepository;

    @Override
    public Specification<WebTradeDetail> getSpecification(WebTradeDetail webtradedetail) {
        return null;
    }

    /**
     * 构建查询语句和参数
     *
     * @param webtradedetail
     * @param parameter
     * @return
     */
    @Override
    public String buildJpql(WebTradeDetail webtradedetail, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder("  from  WebTradeDetail t   ");
        if (webtradedetail != null) {
            WebTradeDetailVo wtdVo = (WebTradeDetailVo) webtradedetail;
            if (StringUtils.isNotBlank(wtdVo.getTrdNo())) {
                jpqlBuilder.append(" and t.trdNo like :trdNo ");
                parameter.put("trdNo", "%".concat(wtdVo.getTrdNo()).concat("%"));
            }
            if (wtdVo.getTrdType() != null) {
                jpqlBuilder.append(" and t.trdType = :trdType ");
                parameter.put("trdType", wtdVo.getTrdType());
            }
            if (wtdVo.getBeginTime() != null) {
                jpqlBuilder.append(" and t.trdTime >= :beginTime ");
                parameter.put("beginTime", wtdVo.getBeginTime());
            }
            if (wtdVo.getEndTime() != null) {
                jpqlBuilder.append(" and t.trdTime <= :endTime ");
                parameter.put("endTime", wtdVo.getEndTime());
            }
        }
        jpqlBuilder.append(" order by trdTime desc ");
        return jpqlBuilder.toString();
    }


    /**
     * 查找所有未执行过余额更正的记录
     *
     * @return
     */
    @Override
    public List<WebTradeDetail> findAllByNotStatistic() {
        return webtradedetailRepository.findByIsStatisticOrderByCreatedDate(false);
    }

    /**
     * 获取网站收支余额
     *
     * @return 返回统计过的余额信息
     */
    @Override
    public BigDecimal findByStatistic() {
        String sql = "select balance from web_trade_detail where is_statistic=1 order by created_date desc limit 1 ";
        Query nativeQuery = em.createNativeQuery(sql);
        nativeQuery.unwrap(SQLQuery.class).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
        if (CollectionUtils.isNotEmpty(nativeQuery.getResultList())) {
            Map map = (Map) nativeQuery.getSingleResult();
            return (BigDecimal) map.get("balance");
        }
        return BigDecimal.ZERO;
    }
}
